package com.hllxd.hism.star.basic.service.service;


import com.hllxd.hism.star.basic.service.dao.mapper.doris.FieldMonitorMapper;
import com.hllxd.hism.star.basic.service.model.doris.ChartPointEntity;
import com.hllxd.hism.star.basic.service.model.doris.FieldMonitorChartEntity;
import com.hllxd.hism.star.basic.service.model.doris.FieldMonitorEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * 消费报告
 *
 * @author lixiyong
 * @date 2023/12/16 16:15
 */
@Slf4j
@Service("fieldMonitorService")
public class FieldMonitorServiceImpl implements FieldMonitorService {

    final static String ZERO = "0";
    private static final Long KB = 1024L;
    private static final Long MB = 1024L * 1024L;
    private static final Long GB = 1024L * 1024L * 1024L;
    private static final Long TB = 1024L * 1024L * 1024L * 1024L;
    private static final Long PB = 1024L * 1024L * 1024L * 1024L * 1024L;

    @Autowired
    private FieldMonitorMapper fieldMonitorMapper;

    @Override
    public List<FieldMonitorEntity> loadMetaData(String biz,String actionType,String logsource,String metric) {

        String[] dtArr = getYearInterval();
//        double maxMetricVal7dAvg = 0;
        log.info("params: {}, {}, biz:{}, actionType: {}, metric: {}, logsource: {}.", dtArr[0],dtArr[1],biz, actionType, metric, logsource);
        if(biz.equals("0")) {
            biz = null;
        }
        if(actionType.equals("0")) {
            actionType = null;
        }
        if(logsource.equals("0")) {
            logsource = null;
        }
        if(metric.equals("0")) {
            metric = null;
        }

        return fieldMonitorMapper.loadMetaData(dtArr[0], dtArr[1], biz, actionType, logsource, metric);
    }

    @Override
    public FieldMonitorChartEntity loadMetricTrend(String biz,String actionType,String logsource,String metric) {

        String[] dtArr = getYearInterval();
//        double maxMetricVal7dAvg = 0;
        log.info("params: {}, {}, metric: {}.", dtArr[0],dtArr[1],metric);

        List<FieldMonitorEntity> list = fieldMonitorMapper.loadMetricTrend(dtArr[0], dtArr[1], biz, actionType, logsource, metric);
//        for (FieldMonitorEntity entity : list) {
//            double metricVal = entity.metricVal;
//            double metricVal7dAvg = entity.metricVal7dAvg;
//            maxMetricVal7dAvg = Math.max(maxMetricVal7dAvg, metricVal7dAvg);
//        }
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<ChartPointEntity> values = new ArrayList<>();

        ChartPointEntity points = new ChartPointEntity();
        points.name = "当日值";
        points.data = new ArrayList<>();
        values.add(points);
        ChartPointEntity points7d = new ChartPointEntity();
        points7d.name = "7日均值";
        points7d.data = new ArrayList<>();
        values.add(points7d);


        int valueIndex = 0;
        if (list.size() >= 1) {
            String startDateStr = list.get(0).getDt();
            LocalDate startDate = LocalDate.parse(startDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDateTime newDataTime = LocalDateTime.now().minusDays(1);
            long until = startDate.until(newDataTime, ChronoUnit.DAYS);

            for (int i = 0; i <= until; i++) {
                LocalDate localDate = startDate.plusDays(i);
                String dateStr = localDate.toString();
                String dbDateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                xList.add(dateStr);
                if (list.size() > valueIndex) {
                    FieldMonitorEntity entity = list.get(valueIndex);
                    if (dbDateStr.equals(entity.getDt())) {
                        points.data.add(entity.metricVal);
                        points7d.data.add(entity.metricVal7dAvg);
                        valueIndex++;
                    } else {
                        points.data.add(null);
                        points7d.data.add(null);
                    }
                } else {
                    points.data.add(null);
                    points7d.data.add(null);
                }
            }
        }
        FieldMonitorChartEntity entity = new FieldMonitorChartEntity();
        entity.setXAxisData(xList);
        entity.setValues(values);
//        entity.setDataUnitName(unitName);
        return entity;
    }

    @Override
    public FieldMonitorChartEntity loadIncMetricTrend(String biz,String actionType,String logsource,String metric) {

        String[] dtArr = getYearInterval();
//        double maxMetricVal7dAvg = 0;
        log.info("params: {}, {}, metric: {}.", dtArr[0],dtArr[1],metric);

        List<FieldMonitorEntity> list = fieldMonitorMapper.loadIncMetricTrend(dtArr[0], dtArr[1], biz, actionType, logsource, metric);
//        for (FieldMonitorEntity entity : list) {
//            double metricVal = entity.metricVal;
//            double metricVal7dAvg = entity.metricVal7dAvg;
//            maxMetricVal7dAvg = Math.max(maxMetricVal7dAvg, metricVal7dAvg);
//        }
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<ChartPointEntity> values = new ArrayList<>();

        ChartPointEntity pointsT2Inc = new ChartPointEntity();
        pointsT2Inc.name = "当日增长率（对比t+2日）";
        pointsT2Inc.data = new ArrayList<>();
        values.add(pointsT2Inc);
        ChartPointEntity pointsT8Inc = new ChartPointEntity();
        pointsT8Inc.name = "7日增长率（对比t+8日）";
        pointsT8Inc.data = new ArrayList<>();
        values.add(pointsT8Inc);


        int valueIndex = 0;
        if (list.size() >= 1) {
            String startDateStr = list.get(0).getDt();
            LocalDate startDate = LocalDate.parse(startDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDateTime newDataTime = LocalDateTime.now().minusDays(1);
            long until = startDate.until(newDataTime, ChronoUnit.DAYS);

            for (int i = 0; i <= until; i++) {
                LocalDate localDate = startDate.plusDays(i);
                String dateStr = localDate.toString();
                String dbDateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                xList.add(dateStr);
                if (list.size() > valueIndex) {
                    FieldMonitorEntity entity = list.get(valueIndex);
                    if (dbDateStr.equals(entity.getDt())) {
                        pointsT2Inc.data.add(entity.t2IncRate);
                        pointsT8Inc.data.add(entity.t8IncRate);
                        valueIndex++;
                    } else {
                        pointsT2Inc.data.add(null);
                        pointsT8Inc.data.add(null);
                    }
                } else {
                    pointsT2Inc.data.add(null);
                    pointsT8Inc.data.add(null);
                }
            }
        }
        FieldMonitorChartEntity entity = new FieldMonitorChartEntity();
        entity.setXAxisData(xList);
        entity.setValues(values);
//        entity.setDataUnitName(unitName);
        return entity;
    }
    @Override
    public FieldMonitorChartEntity loadRatioMetricTrend(String biz,String actionType,String logsource,String metric) {

        String[] dtArr = getYearInterval();
//        double maxMetricVal7dAvg = 0;
        log.info("params: {}, {}, metric: {}.", dtArr[0],dtArr[1],metric);

        List<FieldMonitorEntity> list = fieldMonitorMapper.loadRatioMetricTrend(dtArr[0], dtArr[1], biz, actionType, logsource, metric);
//        for (FieldMonitorEntity entity : list) {
//            double metricVal = entity.metricVal;
//            double metricVal7dAvg = entity.metricVal7dAvg;
//            maxMetricVal7dAvg = Math.max(maxMetricVal7dAvg, metricVal7dAvg);
//        }
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<ChartPointEntity> values = new ArrayList<>();

        ChartPointEntity pointsT1Ratio = new ChartPointEntity();
        pointsT1Ratio.name = "当日数据量占比（对比总量）";
        pointsT1Ratio.data = new ArrayList<>();
        values.add(pointsT1Ratio);

        ChartPointEntity pointsT7Ratio = new ChartPointEntity();
        pointsT7Ratio.name = "7日数据量占比（对比总量）";
        pointsT7Ratio.data = new ArrayList<>();
        values.add(pointsT7Ratio);


        int valueIndex = 0;
        if (list.size() >= 1) {
            String startDateStr = list.get(0).getDt();
            LocalDate startDate = LocalDate.parse(startDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDateTime newDataTime = LocalDateTime.now().minusDays(1);
            long until = startDate.until(newDataTime, ChronoUnit.DAYS);

            for (int i = 0; i <= until; i++) {
                LocalDate localDate = startDate.plusDays(i);
                String dateStr = localDate.toString();
                String dbDateStr = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                xList.add(dateStr);
                if (list.size() > valueIndex) {
                    FieldMonitorEntity entity = list.get(valueIndex);
                    if (dbDateStr.equals(entity.getDt())) {
                        pointsT1Ratio.data.add(entity.d1Rate);
                        pointsT7Ratio.data.add(entity.d7Rate);
                        valueIndex++;
                    } else {
                        pointsT1Ratio.data.add(null);
                        pointsT7Ratio.data.add(null);
                    }
                } else {
                    pointsT1Ratio.data.add(null);
                    pointsT7Ratio.data.add(null);
                }
            }
        }
        FieldMonitorChartEntity entity = new FieldMonitorChartEntity();
        entity.setXAxisData(xList);
        entity.setValues(values);
//        entity.setDataUnitName(unitName);
        return entity;
    }

    private String[] getYearInterval() {
        LocalDateTime time = LocalDateTime.now();
        LocalDateTime time2 = time.minusDays(366);
        String startTime = time2.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String endTime = time.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        return new String[]{startTime, endTime};
    }
}
